/*
 * Created by Yong.Kan @2012-11-17
 */
package com.jiandan.business.service.selfservice;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jiandan.business.entity.selfservice.CemeteryInfoPlane;
import com.jiandan.business.entity.selfservice.CemeteryRegion;
import com.jiandan.business.entity.selfservice.CemeteryRow;
import com.jiandan.business.entity.selfservice.CemeteryUnit;
import com.jiandan.business.entity.selfservice.Machine;
import com.jiandan.business.entity.selfservice.People;
import com.jiandan.business.entity.selfservice.QueryParam;
import com.jiandan.business.repository.selfservice.SelfServiceDao;

// TODO: Auto-generated Javadoc
/**
 * The Class SelfServiceImpl.
 */
@Service
@Transactional(readOnly = true)
public class SelfServiceImpl implements SelfService {

	/** The dao. */
	@Autowired
	private SelfServiceDao dao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.jiandan.business.service.selfservice.SelfService#getAllMachines()
	 */
	@Override
	public List<Machine> getAllMachines() {
		List<Machine> machineList = dao.getAllMachines();
		return machineList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.jiandan.business.service.selfservice.SelfService#getMachines(java
	 * .lang.Integer)
	 */
	@Override
	public Machine getMachines(Integer id) {
		Machine machine = dao.getMachine(id);
		return machine;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.jiandan.business.service.selfservice.SelfService#pinyinAssociate(
	 * java.lang.String)
	 */
	@Override
	public List<String> pinyinAssociate(String pinyin) {
		List<String> chinese = dao.pinyinAssociate(pinyin);
		return chinese;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.jiandan.business.service.selfservice.SelfService#getOneRegionDetail
	 * (java.lang.Integer)
	 */
	@Override
	public CemeteryRegion getOneRegionDetail(Integer regionId) {
		List<CemeteryInfoPlane> plane = dao.getRegionInfo(regionId);
		if (plane.isEmpty()) {
			return null;
		} else {
			List<CemeteryRegion> result = group(plane);
			return result.get(0);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.jiandan.business.service.selfservice.SelfService#getAllRegionNames()
	 */
	@Override
	public List<CemeteryRegion> getAllRegionNames() {
		return dao.getAllRegionNames();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.jiandan.business.service.selfservice.SelfService#getAllRegionDetail()
	 */
	@Override
	public List<CemeteryRegion> getAllRegionDetail() {
		List<CemeteryInfoPlane> plane = dao.getRegionInfo(null);
		List<CemeteryRegion> result = group(plane);
		return result;
	}

	/**
	 * Group.
	 * 
	 * @param plane
	 *            the plane
	 * @return the list
	 */
	private List<CemeteryRegion> group(List<CemeteryInfoPlane> plane) {
		Map<String, CemeteryRegion> regionMap = new LinkedHashMap<String, CemeteryRegion>();
		Map<String, CemeteryRow> rowMap = new HashMap<String, CemeteryRow>();
		for (CemeteryInfoPlane record : plane) {
			CemeteryUnit unit = new CemeteryUnit(record);
			CemeteryRegion region = regionMap.get(record.getRegion_id());
			if (region == null) {
				region = new CemeteryRegion(record);
				regionMap.put(record.getRegion_id(), region);
			}

			CemeteryRow row = rowMap.get(record.getRow_id());
			if (row == null) {
				row = new CemeteryRow(record);
				region.getRows().add(row);
				rowMap.put(record.getRow_id(), row);
			}
			row.getUnits().add(unit);

		}

		List<CemeteryRegion> result = new ArrayList<CemeteryRegion>();
		result.addAll(regionMap.values());
		return result;
	}

	@Override
	public List<People> findPeoples(QueryParam param) {
		return dao.findPeoples(param);
	}

}
